package ru.spb.osll.myfit.client.widget;

import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Grid;

import ru.spb.osll.myfit.common.ProgressCounts;

/*
.progressbarFull {
 background: #33ff33;
}

.progressbarBlank {
 background: #d3d3d3;
}

.progressbarText {
 font-weight: bold;
 color: black;
 font-size: 12px;
}

.progressbarBorder {
 border-style: solid;
 border-color: black;
 border-width: 1;
}
 */

public class ProgressBar extends HorizontalPanel {
	private int m_elements;
	private Grid m_grid;
	private Label m_label = new Label();

	public ProgressBar() {
		this(25);
	}

	public ProgressBar(int elements) {
		setVisible(false);
		setSpacing(5);

		m_elements = elements;
		m_grid = new Grid(1, m_elements);
		m_grid.setCellPadding(0);
		m_grid.setCellSpacing(0);

		for(int i = 0; i < m_elements; i++) {
			Grid e = new Grid(1, 1);
			e.setStyleName("progressbarBlank");
			m_grid.setWidget(0, i, e);
		}

		m_grid.setWidth("100%");
		m_grid.setStyleName("progressbarBorder");
		m_label.setStyleName("progressbarText");

		add(m_label);		
		add(m_grid);
	}

	public void updateView(ProgressCounts pCount) {
		int total = pCount.getTotalSegments();
		int progress = pCount.getProgressSegments();
		int right = pCount.getRight();
		int wrong = pCount.getWrong();
		int ignored = pCount.getIgnores();
		int exceptions = pCount.getExceptions();

		double persentage = (double) progress / total;
		int progressCount = (int) (m_elements * persentage);

		for(int i = 0; i < m_elements; i++) {
			Grid e = (Grid) m_grid.getWidget(0, i);
			if(i < progressCount)
				e.setStyleName("progressbarFull");
			else
				e.setStyleName("progressbarBlank");
		}

		String labelText = new String("processed: " + progress + ", right: " + right + 
						", wrong: " + wrong + ", ignored: " + ignored +
						", exceptions: " + exceptions);
		m_label.setText(labelText);
	}
}
